package com.goal98.team.services

import com.infusion.util.event.EventBroker
import com.infusion.util.event.EventBrokerHolder
import com.goal98.team.Constants.Events
import com.goal98.team.Constants

class EventConsumerService {

    def emailerService
    def authenticateService


    boolean transactional = false

    def init() {
        EventBroker eventBroker = EventBrokerHolder.getEventBroker()
        eventBroker.subscribe(Constants.Events.USER_ACTIVATED.toString()) {data, broker ->
            this.sendRegEmail(data)
        }

        eventBroker.subscribe(Constants.Events.ACTIVITY_COST_DEBITED.toString()) {data, broker ->
            this.sendAttendeeCostDebitEmail(data)
        }
    }

    def sendRegEmail(data) {
        if (authenticateService.securityConfig.security.useMail) {
            def email = [
                    to: [data.to],
                    subject: data.subject,
                    text: data.emailContent
            ]
            emailerService.sendHtmlEmails([email])

        }

    }

    def sendAttendeeCostDebitEmail(data){
        //TODO: Send each attendee a email about the activity details and amount of money debit from his account
    }

}
